package com.haierp.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.imageio.ImageIO;

import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Sets;
import com.haierp.enums.OrderStatus;
import com.haierp.exception.InventoryException;
import com.haierp.mapper.MistakeMapper;
import com.haierp.model.ErpOrder;
import com.haierp.model.ErpReturnOrder;
import com.haierp.model.Mistake;
import com.haierp.model.ShippingOrder;
import com.haierp.service.IErpOrderService;
import com.haierp.service.IErpReturnOrderService;
import com.haierp.service.IShippingOrderService;
import com.haierp.util.DateUtil;
import com.haierp.util.JsonPageResult;
import com.haierp.util.JsonResult;
import com.haierp.util.ExcelTool.ExcelHelper;
import com.haierp.vo.ErpReturnOrderQueryVO;
import com.haierp.vo.json.PicModel;
import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.shiro.ShiroUser;
import com.wangzhixuan.commons.utils.HaiJsonUtils;

/**
 * @author liuyang
 *
 */
@Controller
@RequestMapping("/erpReturnOrder")
public class ErpReturnOrderController extends BaseController {
	@Autowired
	private IErpReturnOrderService erpReturnOrderService;
	@Autowired
	private IErpOrderService erpOrderService;
	@Autowired
	private IShippingOrderService shippingOrderService;
	@Autowired
	private MistakeMapper mistakeMapper;
	
	@RequestMapping("/query")
	@ResponseBody
	public Object query(ErpReturnOrderQueryVO erpReturnOrderQueryVO) {
		JsonPageResult<List<ErpReturnOrder>> result = new JsonPageResult<>();
		if(erpReturnOrderQueryVO.getEndGmtCreate()!=null) {
			String endGmtCreateStr = DateUtil.ymdFormat(erpReturnOrderQueryVO.getEndGmtCreate());
			Date endGmtCreate = DateUtil.parseDate(endGmtCreateStr + " 23:59:59");
			erpReturnOrderQueryVO.setEndGmtCreate(endGmtCreate);
		}		
		ShiroUser shiroUser = this.getShiroUser();
		Set<String> roles = shiroUser.getRoles();
		result = erpReturnOrderService.queryErpReturnOrders(erpReturnOrderQueryVO);
		if(roles.contains("irhdaili")) {
			result.setAgentRoler(true);
		}
		if(roles.contains("noexport")) {
			result.setNoEexportRoler(true);
		}
		result.setSuccess(true);
		return result;
	}
	
	@RequestMapping("/queryById")
	@ResponseBody
	public Object queryById(Long id) {
		JsonResult<ErpReturnOrder> result = new JsonResult<>();
		ErpReturnOrder erpReturnOrder = erpReturnOrderService.selectById(id);
		if(erpReturnOrder == null) {
			return result.buildIsSuccess(false).buildMsg("查询异常");
		}
		ErpOrder erpOrder = erpOrderService.selectById(erpReturnOrder.getErpOrderId());
		erpReturnOrder.setItemName(erpOrder.getItemName());
		erpReturnOrder.setSkuCode(erpOrder.getSkuCode());
		erpReturnOrder.setUpc(erpOrder.getUpc());
		erpReturnOrder.setSkuPic(erpOrder.getSkuPic());
		result.buildIsSuccess(true).setData(erpReturnOrder);
		return result;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Object add(ErpReturnOrder erpReturnOrder) throws InventoryException {
		ShiroUser one = this.getShiroUser();
		erpReturnOrder.setCompanyId(one.getCompanyId());
		JsonResult<ErpReturnOrder> result = new JsonResult<>();
		if(erpReturnOrder.getErpOrderId()==null) {
			return result.buildIsSuccess(false).buildMsg("参数异常");
		}
		if (erpReturnOrder.getReturnReason().equals("录单错误") && erpReturnOrder.getCompanyId() == 1L) {
			ErpOrder erpOrder = erpOrderService.selectById(erpReturnOrder.getErpOrderId());
			if (erpOrder.getStatus() == OrderStatus.SENT.getCode()) { // 只有已发货的计入统计
				Mistake mitake = new Mistake();
				mitake.setErrorErpId(erpOrder.getId());
				mitake.setErrorErpNo(erpOrder.getErpNo());
				mitake.setErrorMan(erpOrder.getUserModify());
				mitake.setErrorReason(erpReturnOrder.getReturnReason());
				mitake.setUserCreate(one.getLoginName());
				mitake.setUserModify(one.getLoginName());
				mitake.setGmtCreate(erpOrder.getGmtCreate());
				mitake.setGmtModify(new Date());
				erpOrder.setStatus(-2);
				erpOrder.setCloseReason("建单出错");
				erpOrderService.updateById(erpOrder);
				mistakeMapper.insert(mitake);
			}
		}
		;
		if ((erpReturnOrder.getReturnReason().equals("发货错误") || erpReturnOrder.getReturnReason().equals("打包错误"))
				&& erpReturnOrder.getCompanyId() == 1L) {
			ErpOrder erpOrder = erpOrderService.selectById(erpReturnOrder.getErpOrderId());
			if (erpOrder.getStatus() == OrderStatus.SENT.getCode()) {
				ShippingOrder shippingorder = shippingOrderService.selectById(erpOrder.getShippingOrderId());
				if (shippingorder != null) {
					Mistake mitake = new Mistake();
					mitake.setErrorErpId(erpOrder.getId());
					mitake.setErrorErpNo(erpOrder.getErpNo());
					if (erpReturnOrder.getReturnReason().equals("发货错误")) {
						mitake.setErrorMan(shippingorder.getUserCreate());
						mitake.setGmtCreate(shippingorder.getGmtCreate());// 发货发错时间
						erpOrder.setCloseReason("发货出错");
					} else {
						mitake.setErrorMan(shippingorder.getPackageCreate());
						mitake.setGmtCreate(shippingorder.getPackageTime());// 打包出错时间
						erpOrder.setCloseReason("打包出错");
					}
					mitake.setErrorReason(erpReturnOrder.getReturnReason());
					mitake.setUserCreate(one.getLoginName());
					mitake.setUserModify(one.getLoginName());
					mitake.setGmtModify(new Date());
					erpOrder.setStatus(-2);
					erpOrderService.updateById(erpOrder);
					mistakeMapper.insert(mitake);
				}
			}
		}
		;
		erpReturnOrder.setReturnRefer(0);
		erpReturnOrderService.add(erpReturnOrder);
		result.setSuccess(true);
		return result;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Object update(ErpReturnOrder erpReturnOrder) {
		JsonResult<ErpReturnOrder> result = new JsonResult<>();
		ShiroUser shiroUser = this.getShiroUser();
		erpReturnOrder.setUserModify(shiroUser.getLoginName());
		erpReturnOrder.setGmtModify(new Date());
		erpReturnOrderService.updateSelectiveById(erpReturnOrder);
		if(erpReturnOrder.getStatus() == -1) {	//关闭退单
			ErpReturnOrder selErpReturnOrder = erpReturnOrderService.selectById(erpReturnOrder.getId());
			//恢复子订单状态
			ErpOrder selErpOrder = erpOrderService.selectById(selErpReturnOrder.getErpOrderId());
			if(selErpOrder.getStatus() == -1) {
				selErpOrder.setStatus(0);
				selErpOrder.setCloseReason("");
				erpOrderService.updateSelectiveById(selErpOrder);
				//对子订单分配库存
				try {
					erpOrderService.lockErpOrder(selErpOrder);
				} catch (InventoryException e) {
					logger.error("关闭退单", e);
				}
				
				Set<Long> mainIds = Sets.newHashSet();
				mainIds.add(selErpOrder.getOuterOrderId());
				shippingOrderService.updateOuterstatus(mainIds);
			}
		}
		result.setSuccess(true);
		return result;
	}
	/**
	 * 根据退单导出excel表
	 */
	@RequestMapping("/erpReturnOrderExport")
    public ResponseEntity<byte[]> erpOrderExport(ErpReturnOrderQueryVO erpReturnOrderQueryVO) throws Exception {
    	List<List<Object>> rowDatas = new ArrayList<>();
    	if(erpReturnOrderQueryVO.getStartGmtCreate() != null) {
    		String startGmtCreateStr = DateUtil.ymdFormat(erpReturnOrderQueryVO.getStartGmtCreate());
    		Date startGmtCreate= DateUtil.parseDate(startGmtCreateStr + " 00:00:00");
    		erpReturnOrderQueryVO.setStartGmtCreate(startGmtCreate);
    	}
    	if(erpReturnOrderQueryVO.getEndGmtCreate() != null) {
    		String endGmtCreateStr = DateUtil.ymdFormat(erpReturnOrderQueryVO.getEndGmtCreate());
    		Date endGmtCreate = DateUtil.parseDate(endGmtCreateStr + " 23:59:59");
    		erpReturnOrderQueryVO.setEndGmtCreate(endGmtCreate);
    	}
		
    	 List<ErpReturnOrder> erpReturnOrderList = erpReturnOrderService.queryErpReturnOrderForExcel(erpReturnOrderQueryVO);
    	if(erpReturnOrderList != null) {
    		for (ErpReturnOrder erpReturnOrder : erpReturnOrderList) {
    			List<Object> list = new ArrayList<>();
    			//sku图片
    			String skuPic = erpReturnOrder.getSkuPic();
    			if(StringUtil.isNotBlank(skuPic)) {
    				PicModel pm = HaiJsonUtils.toBean(skuPic, PicModel.class);
        			String imgSrc = pm.getPicList().get(0).getUrl();
        			String imgType = imgSrc.substring(imgSrc.lastIndexOf(".") + 1).toUpperCase();
        			if(imgSrc.contains("aliyuncs.com")) {
        				imgSrc += "?x-oss-process=image/resize,m_lfit,h_100,w_100";
        			} else {
        				imgSrc = imgSrc.replaceAll("Resource", "Thumbnail");
        			}
        			//System.out.println(imgSrc);
        			URL url = new URL(imgSrc);
        			BufferedImage image = ImageIO.read(url);
        			ByteArrayOutputStream output = new ByteArrayOutputStream();
        	        ImageIO.write(image, imgType, output);
        	        output.flush();
        	        list.add(output.toByteArray());
        	        output.close();
    			} else {
    				list.add(null);
    			}
    			
    	        list.add(erpReturnOrder.getItemName());	     //商品名称    	    
    	        list.add(erpReturnOrder.getOrderNo());	     //主订单号
    	        list.add(erpReturnOrder.getErpNo());	     //子订单号
    	        list.add(erpReturnOrder.getSkuCode());	     //SKU代码    	
    	        list.add(erpReturnOrder.getUpc());           //upc
    	        list.add(erpReturnOrder.getReturnReason());	 //退单原因
    	        list.add(erpReturnOrder.getReturnReasonDetail());	 //退单原因详情
    	        list.add(OrderStatus.of(erpReturnOrder.getStatus()).getDescription());	//状态             	            	             
    	        list.add(erpReturnOrder.getReturnQuantity());	     //退货数量
    	        list.add(erpReturnOrder.getReturnPrice());           //退款金额
    	        list.add(erpReturnOrder.getIsGn());                  //是否国内退货
    	        list.add(erpReturnOrder.getIsCheckin());             //是否入库
    	        list.add(DateUtil.ymdFormat(erpReturnOrder.getReceiveTime()));          //收货时间
    	        list.add(DateUtil.ymdFormat(erpReturnOrder.getReturnPayTime()));        //退款时间
    	        list.add(erpReturnOrder.getSalesName());             //售货员
    	        list.add(erpReturnOrder.getRemark());		         //备注
    	        
    			rowDatas.add(list);
    		}
    	}
    	ExcelHelper excelHelper = new ExcelHelper();
    	String[] columnTitles = new String[]{"sku图片", "商品名称", "主订单号", "子订单号", "SKU代码", "upc", "退单原因",  "退单原因详情", "状态", "退货数量", "退款金额", "是否国内退货", "是否入库", "收货时间", "退款时间", "销售员", "备注"};
    	Integer[] columnWidth = new Integer[]{10, 30, 20, 20, 20, 20, 20, 20, 15, 15, 15, 15, 15, 20, 15, 10, 20};
    	excelHelper.setErpReturnOrderToSheet("ErpReturnOrder", columnTitles, rowDatas, columnWidth);
    	
    	ResponseEntity<byte[]> filebyte = null;
    	ByteArrayOutputStream  out = excelHelper.writeToByteArrayOutputStream();
    	HttpHeaders headers = new HttpHeaders();
    	headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    	String fileName = "退货单.xlsx";
        headers.setContentDispositionFormData("attachment", new String(fileName.getBytes("utf-8"), "ISO8859-1"));
        
        filebyte = new ResponseEntity<byte[]>(out.toByteArray(), headers, HttpStatus.OK);
        out.close();
        excelHelper.close();
        return filebyte;
    }
}
